package com.celesea.auth.position.vo;

import com.celesea.auth.position.entity.Position;
import com.celesea.auth.position.v1.controller.request.ConfigRequest;
import com.celesea.auth.position.v1.controller.request.PostRequest;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.PinyinKit;
import com.celesea.framework.vo.DictionaryVo;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-10-29 22:25
 */
public class PositionVo extends Position {
    private DictionaryVo dept;
    private DictionaryVo currentDept;

    private List<UserPositionVo> users=Lists.newArrayList();

    public PositionVo() {
    }

    public PositionVo(PostRequest request) {
        BeanUtils.copyProperties(request, this);
        this.setCompany(EnvUtil.getCompany());
        this.setPy(PinyinKit.getPinyinFirst(this.getName()));
        this.setDept(request.getDept());
        if (request.getDept() != null) {
            this.setDeptId(request.getDept().getId());
            this.setDeptName(request.getDept().getName());
        }
        this.setBubble(Const.Bool.FALSE);
    }


    public PositionVo(ConfigRequest request) {
        BeanUtils.copyProperties(request, this);
        if (CollectionKit.isNotEmpty(request.getUsers())) {
            this.users = request.getUsers().stream().map(item ->
                    new UserPositionVo(this, item)).collect(Collectors.toList());
        }
    }

    public PositionVo(Position position) {
        BeanUtils.copyProperties(position, this);
    }

    public Position toEntity() {
        Position entity = new Position();
        BeanUtils.copyProperties(this, entity);
        return entity;
    }

    public void initForCreate(DictionaryVo dept) {
        this.setExtend(Const.Bool.TRUE);
        this.setBubble(Const.Bool.TRUE);
        this.setStatusFlag(Const.Bool.TRUE);
        this.users = Lists.newArrayList();
        this.dept = dept;
    }

    public DictionaryVo getDept() {
        return dept;
    }

    public void setDept(DictionaryVo dept) {
        this.dept = dept;
    }

    public List<UserPositionVo> getUsers() {
        return users;
    }

    public void setUsers(List<UserPositionVo> users) {
        this.users = users;
    }

    public DictionaryVo getCurrentDept() {
        return currentDept;
    }

    public void setCurrentDept(DictionaryVo currentDept) {
        this.currentDept = currentDept;
    }
}
